package org.dimock.proxy;

import java.lang.reflect.Proxy;


public class DIMocker {
	private DependencyResolver dependencyResolver = new DefaultDependencyResolver();
	private MethodResolver methodResolver = new DefaultMethodResolver();

	public  final void inject(Object subject, Object resolver){
		inject(subject, resolver, false, false);
	}
	
	private  Object newInstance(ProxyContext context, Object target) {
		Class<?>[] interfaces = dependencyResolver.getAllMockableDependencies(target.getClass());
		ClassLoader cl = target.getClass().getClassLoader();
		DefaultProxy proxy = new DefaultProxy(context);
		Object wrappedProxy = Proxy.newProxyInstance(cl, interfaces, proxy);
		dependencyResolver.injectMockableDependencies(target, wrappedProxy);
		return wrappedProxy;
	}

	
	public  void inject(Object subject, Object resolver, boolean recursive, boolean strict){
		ProxyContext context = new ProxyContext();
		context.setRecursive(recursive);
		context.setRedirect(resolver);
		context.setStrict(strict);
		context.setMethodResolver(methodResolver);
		context.setDependencyResolver(dependencyResolver);
		newInstance(context, subject);
	}

	public void setDependencyResolver(DependencyResolver dependencyResolver) {
		this.dependencyResolver = dependencyResolver;
	}

	public void setMethodResolver(MethodResolver methodResolver) {
		this.methodResolver = methodResolver;
	}

	
}
